/*

	objx Inversion of Control plugin
	Copyright (c) 2008 Mat Ryer
	
	Please visit the project home at http://objx.googlecode.com/

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.

*/

var objx = objx || {};

objx.ioc = {

	Container: Class({
	
		mappings: null,
		instances: null,
		inject: true,
		
		init: function(){
			this.mappings = {};
			this.instances = {};
		},
		
		has: function(k){

			if (this.mappings[k]) 
				return true; 
			else
				return false;

		},
		
		set: function(k, v){

			if (typeof k != "string") throw "Must pass string as key to objx.ioc.Container.set method";

			// create a mapping object
			var o = {};

			// is the value a class?
			if (v && objx.isc(v)){ 
				// set the class
				o.klass = v;
			}
			else
				// assume it's an instance of a class (or other object)
				o.instance = (v || null);

			// make the mapping
			this.mappings[k] = o;

			// create the shortcut getter method
			this["get" + k] = function(){ return this.get(k); };
			
			// return the container for chaining
			return this;
			
		},
		
		get: function(k){
		
			// get the mapping object
			var o = this.mappings[k];

			if (!o) return null;

			// is it a class?
			if (o.klass){
			
				// return the class
				return this.getOrMakeInstance(k, o.klass);				
			
			}
			else {
				return o.instance;
			}

		},
		
		getOrMakeInstance: function(k, klass){
			
			// do we already have this?
			if (!this.instances[k])
			{
				// make it
				this.instances[k] = this.getInstance(klass);
			}
			
			// return the instance
			return this.instances[k];
			
		},
		
		getInstance: function(klass){
		
			if (this.inject) {
			
				// get the constructor arguments
				var constructorArgs = objx.args(klass.prototype.init);
	
				var injectionArgs = new Array();
	
				// loop through each constructor argument
				for (var i = 0, l = constructorArgs.length; i < l; i++) {
					
					var arg = constructorArgs[i];
					
					if (arg.substring(0,1) == "_")
						injectionArgs[i] = this.get(arg.substring(1));
					else if (arg.substring(0,6) == "query_")
					{
						// get args from querystring
						injectionArgs[i] = objx.qs()[arg.substring(6)];
					}
					else
						injectionArgs[i] = null;

				};

				// supress the 'init' method - we will call it
				klass.supressInit = true;
				
				// instantiate the class
				var i = new klass();
				
				// call the constructor manually
				i.init.apply(i, injectionArgs);
				
				// stop supressing the 'init' method
				klass.supressInit = false;
				
				// return the instance
				return i;
			
			}
			else
			{
				return new klass();
			}

		}
	
	})

}